package player.sucker 
{
	import flash.geom.Point;
	import flash.media.Sound;
	import org.flixel.FlxBasic;
	import org.flixel.FlxObject;
	import org.flixel.FlxSprite;
	import org.flixel.FlxPoint;
	import org.flixel.FlxSound;
	import org.flixel.FlxG;
	/**
	 * ...
	 * @author João Brant
	 */
	public class Vortex extends FlxSprite
	{
		[Embed(source = "../../../assets/vooooshhh.png")]
		public static var vortexImage:Class;
	
		[Embed(source="../../../assets/som/wind.mp3")]
		public static var vortexSound:Class;
		
		private var soundCount:int = 0;
		
		public static var vortexCollisionPoints:Array = 
		[
			new FlxPoint(0, 250),
			new FlxPoint(0, 225),
			new FlxPoint(0, 192),
			new FlxPoint(0, 168),
			new FlxPoint(0, 128),
			new FlxPoint(0, 64),
			new FlxPoint(-60, 240),
			new FlxPoint(60, 240),
			new FlxPoint(50, 192),
			new FlxPoint( -50, 192),
			new FlxPoint(40, 150),
			new FlxPoint( -40, 150),
			new FlxPoint(20, 100),
			new FlxPoint( -20, 100),
			new FlxPoint(0, 20)
		];
		
		
		public static const vortexBaseHeight:int = 200;
		public static const vortexBaseWidth:int = 100;
		
		private var start:FlxPoint = new FlxPoint();
		private var target:FlxPoint;
		
		public function Vortex(ori:FlxSprite, targ:FlxPoint) 
		{
			origin.x = 64;
			origin.y = 256;
			loadGraphic(vortexImage, true, false, 128, 256);
			addAnimation("suck", [0, 1, 2, 3, 4, 5], 15, true);
			play("suck");
			alpha = 0.6;
			//start.x = ori.x + 16;
			//start.y = ori.y + 32;
			target = targ;
			//vortexSound = new FlxSound();
			//vortexSound.loadEmbedded(vortexSoundClass, false);
		}
		
		public override function update():void
		{
			super.update();
			
			updatePosition();
		}
		
		public function updatePosition():void
		{
			if (start != null && target != null)
			{
				origin.x = 64;
				origin.y = 256;
				
				x = start.x - origin.x;
				y = start.y - origin.y;
				
				var sin:Number;
				
				if (target.y < start.y)
				{
					//trace(target.x + " " + start.x +" " + ((target.x - start.x) / (Math.sqrt((target.x - start.x) * (target.x - start.x) + (target.y - start.y) * (target.y - start.y)))));
					sin = (target.x - start.x) / (Math.sqrt((target.x - start.x) * (target.x - start.x) + (target.y - start.y) * (target.y - start.y)));
					angle = (Math.asin(sin) * 180 / Math.PI);
					
					/*if (angle > 0 && angle < 45)
						angle = 45;
					else if (angle < 0 && angle > -45)
						angle = -45;*/
				}
				else
				{
					sin = (target.x - start.x) / (Math.sqrt((target.x - start.x) * (target.x - start.x) + (target.y - start.y) * (target.y - start.y)));
					angle = 180 - (Math.asin(sin) * 180 / Math.PI);
					
					/*if (angle < 180 && angle > 135)
						angle = 135;
					else if (angle > 180 && angle < 225)
						angle = 225;*/
				}
			}
		}
		
		public function emitSound():void
		{
			if (soundCount % 7 == 0)
			{
				//var flashSound:Sound = new vortexSoundClass();
				//flashSound.play();
				FlxG.play(vortexSound);
			}
			soundCount++;
		}
		
		public function setTargets(ori:FlxSprite, targ:FlxPoint):void
		{
			start.x = ori.x + 16;
			start.y = ori.y;
			target = targ;
		}
	
	}

}